package cn.csdj.sism.web;

import cn.csdj.sism.pojo.Student;
import cn.csdj.sism.service.StudentService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller实现类
 * 李心怡
 * 2023/4/10
 */
@CrossOrigin
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @PostMapping
    public int insert(@RequestBody Student student){
        //如果为空就是添加
        if (StringUtils.isEmpty(student.getStudentId())){
            return studentService.insert(student);
        }else {
//            如果有值就是修改
            return studentService.update(student);
        }
    }
    @DeleteMapping("/{id}")
    public int deleteById(@PathVariable Integer id){
        return studentService.deleteById(id);
    }
    @GetMapping("/{id}")
    public Student selectById(@PathVariable Integer id){
        return studentService.selectById(id);
    }
    @GetMapping
    public List<Student> selectAll(){
        return studentService.selectAll();
    }
    @GetMapping("/page/{current}/{pageSize}")
    public IPage<Student> selectPage(@PathVariable Integer current,@PathVariable Integer pageSize){
        return studentService.selectPages(current,pageSize);
    }
    //按班级id分页查询
    @GetMapping("/page/{current}/{pageSize}/{classId}")
    public IPage<Student> selectPageByClassId(@PathVariable Integer current,@PathVariable Integer pageSize,@PathVariable Integer classId){
        return studentService.selectPagesByClassId(current,pageSize,classId);
    }
    @PostMapping("/condition")
    public IPage<Student> selectPageByClassId(@RequestBody HashMap condition){
        return studentService.selectPagesByCondition(condition);
    }
}
